﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net.Mail;
using System.Configuration;
/// <summary>
/// some internal tools
/// </summary>
public static class Helper
{
    public static void WriteLog(string message)
    {
        if (File.Exists("Log.txt"))
        {
            using (FileStream fs = new FileStream("Log.txt", FileMode.Append, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine(message);
                }
            }
        }
        else
        {
            File.WriteAllText("Log.txt", message);
        }
    }
    public static Connector GetConnector(string type)
    {
        switch (type)
        {
            case "Active Directory":
                return new ActiveDirectory();
            case "OpenLDAP":
                return new OpenLDAP();
            case "Azure AD":
                return new AzureAD();
            default:
                break;
        }
        return null;
    }
    public static void SendEmail(string To, string subject, string Message)
    {
        SmtpClient SmtpServer = new SmtpClient(ConfigurationManager.AppSettings["SMTPServer"]);
        SmtpServer.Port = Convert.ToInt32((ConfigurationManager.AppSettings["SMTPServer"]));
        SmtpServer.Credentials = new System.Net.NetworkCredential((ConfigurationManager.AppSettings["SMTPUser"]), (ConfigurationManager.AppSettings["SMTPPassword"]));
        SmtpServer.EnableSsl = true;

        MailMessage mail = new MailMessage();
        mail.From = new MailAddress((ConfigurationManager.AppSettings["SMTPfrom"]));
        mail.SubjectEncoding = System.Text.Encoding.UTF8;
        mail.To.Add(To);
        mail.Subject = subject;
        mail.Body = Message;
        SmtpServer.Send(mail);
    }
}